package com.power.using.sharlockchat.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.style.ToastWhiteStyle;
import com.power.using.sharlockchat.R;
import com.power.using.sharlockchat.interfaces.Constant;
import com.power.using.sharlockchat.utils.SPUtil;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2019/1/23.
 * Time: 2019/1/23  14:22
 */
public class SplashActivity extends Activity {


    private String[] pp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //这里设置为全屏显示
        Window window = getWindow();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        window.setFlags(flag, flag);
        setContentView(R.layout.activity_splash);

        ToastUtils.init(getApplication(), new ToastWhiteStyle());

        boolean isFirstLogin = getCheckFirst();

        pp = new String[]{Manifest.permission.CAMERA, Manifest.permission.REQUEST_INSTALL_PACKAGES,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE,
                Manifest.permission.READ_EXTERNAL_STORAGE

        };
        requestPermission();

    }

    /**
     * 判断是否添加了权限
     */
    public void requestPermission() {
        XXPermissions.with(this)
                .permission(pp) //不指定权限则自动获取清单中的危险权限
                .request(new OnPermission() {

                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        if (isAll) {
                            GotoNextPage();
                        } else {
                            ToastUtils.show("获取权限成功，部分权限未正常授予");
                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        if (quick) {
                            ToastUtils.show("被永久拒绝授权，请手动授予权限");
                            //如果是被永久拒绝就跳转到应用权限系统设置页面
                            //XXPermissions.gotoPermissionSettings(SplashActivity.this);
                        } else {
                            ToastUtils.show("获取权限失败");
                        }
                    }
                });
    }

    private void GotoNextPage() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //跳转的时候设置为非全屏     可以避免跳转到主页的时候,顶部状态栏抖动
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

                if (!getCheckFirst()) {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                } else {
                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    finish();
                }
                //这里拿到保存的账户信息,直接请求接口进行登录,进入主页
            }
        }, 2000);

    }

    /**
     * 是否第一次登录
     *
     * @return
     */
    private boolean getCheckFirst() {
        String username = SPUtil.getString(Constant.KEY_USERNAME, "");
        String userpass = SPUtil.getString(Constant.KEY_USERPWD, "");
        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(userpass) && username != null && userpass != null) {
            return false;
        }
        return true;
    }
}
